
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/physics/quantum/grover.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:27:43 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Grover’s Algorithm &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../../_static/sympy-notailtext-favicon.ico"/>
    <link href="grover.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="QFT" href="qft.html" />
    <link rel="prev" title="Gates" href="gate.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="qft.html" title="QFT"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="gate.html" title="Gates"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../index.html" >Physics</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="index.html" accesskey="U">Quantum Mechanics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Grover’s Algorithm</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.physics.quantum.grover">
<span id="grover-s-algorithm"></span><h1>Grover’s Algorithm<a class="headerlink" href="#module-sympy.physics.quantum.grover" title="Permalink to this headline">¶</a></h1>
<p>Grover’s algorithm and helper functions.</p>
<p>Todo:</p>
<ul class="simple">
<li><p>W gate construction (or perhaps -W gate based on Mermin’s book)</p></li>
<li><p>Generalize the algorithm for an unknown function that returns 1 on multiple
qubit states, not just one.</p></li>
<li><p>Implement _represent_ZGate in OracleGate</p></li>
</ul>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.OracleGate">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.physics.quantum.grover.</span></span><span class="sig-name descname"><span class="pre">OracleGate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/physics/quantum/grover.py#L58-L178"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.physics.quantum.grover.OracleGate" title="Permalink to this definition">¶</a></dt>
<dd><p>A black box gate.</p>
<p>The gate marks the desired qubits of an unknown function by flipping
the sign of the qubits.  The unknown function returns true when it
finds its desired qubits and false otherwise.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>qubits</strong> : int</p>
<blockquote>
<div><p>Number of qubits.</p>
</div></blockquote>
<p><strong>oracle</strong> : callable</p>
<blockquote>
<div><p>A callable function that returns a boolean on a computational basis.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Apply an Oracle gate that flips the sign of <code class="docutils literal notranslate"><span class="pre">|2&gt;</span></code> on different qubits:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.qubit</span> <span class="kn">import</span> <span class="n">IntQubit</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.qapply</span> <span class="kn">import</span> <span class="n">qapply</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.grover</span> <span class="kn">import</span> <span class="n">OracleGate</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">qubits</span><span class="p">:</span> <span class="n">qubits</span> <span class="o">==</span> <span class="n">IntQubit</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">OracleGate</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qapply</span><span class="p">(</span><span class="n">v</span><span class="o">*</span><span class="n">IntQubit</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="go">-|2&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qapply</span><span class="p">(</span><span class="n">v</span><span class="o">*</span><span class="n">IntQubit</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">|3&gt;</span>
</pre></div>
</div>
<dl class="py property">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.OracleGate.search_function">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">search_function</span></span><a class="headerlink" href="#sympy.physics.quantum.grover.OracleGate.search_function" title="Permalink to this definition">¶</a></dt>
<dd><p>The unknown function that helps find the sought after qubits.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.OracleGate.targets">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">targets</span></span><a class="headerlink" href="#sympy.physics.quantum.grover.OracleGate.targets" title="Permalink to this definition">¶</a></dt>
<dd><p>A tuple of target qubits.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.WGate">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.physics.quantum.grover.</span></span><span class="sig-name descname"><span class="pre">WGate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/physics/quantum/grover.py#L181-L239"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.physics.quantum.grover.WGate" title="Permalink to this definition">¶</a></dt>
<dd><p>General n qubit W Gate in Grover’s algorithm.</p>
<p>The gate performs the operation <code class="docutils literal notranslate"><span class="pre">2|phi&gt;&lt;phi|</span> <span class="pre">-</span> <span class="pre">1</span></code> on some qubits.
<code class="docutils literal notranslate"><span class="pre">|phi&gt;</span> <span class="pre">=</span> <span class="pre">(tensor</span> <span class="pre">product</span> <span class="pre">of</span> <span class="pre">n</span> <span class="pre">Hadamards)*(|0&gt;</span> <span class="pre">with</span> <span class="pre">n</span> <span class="pre">qubits)</span></code></p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>nqubits</strong> : int</p>
<blockquote>
<div><p>The number of qubits to operate on</p>
</div></blockquote>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.apply_grover">
<span class="sig-prename descclassname"><span class="pre">sympy.physics.quantum.grover.</span></span><span class="sig-name descname"><span class="pre">apply_grover</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">oracle</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nqubits</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterations</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/physics/quantum/grover.py#L280-L323"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.physics.quantum.grover.apply_grover" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies grover’s algorithm.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>oracle</strong> : callable</p>
<blockquote>
<div><p>The unknown callable function that returns true when applied to the
desired qubits and false otherwise.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><strong>state</strong> : Expr</p>
<blockquote>
<div><p>The resulting state after Grover’s algorithm has been iterated.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Apply grover’s algorithm to an even superposition of 2 qubits:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.qapply</span> <span class="kn">import</span> <span class="n">qapply</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.qubit</span> <span class="kn">import</span> <span class="n">IntQubit</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.grover</span> <span class="kn">import</span> <span class="n">apply_grover</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">qubits</span><span class="p">:</span> <span class="n">qubits</span> <span class="o">==</span> <span class="n">IntQubit</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qapply</span><span class="p">(</span><span class="n">apply_grover</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">|2&gt;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.grover_iteration">
<span class="sig-prename descclassname"><span class="pre">sympy.physics.quantum.grover.</span></span><span class="sig-name descname"><span class="pre">grover_iteration</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">qstate</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">oracle</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/physics/quantum/grover.py#L242-L277"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.physics.quantum.grover.grover_iteration" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies one application of the Oracle and W Gate, WV.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>qstate</strong> : Qubit</p>
<blockquote>
<div><p>A superposition of qubits.</p>
</div></blockquote>
<p><strong>oracle</strong> : OracleGate</p>
<blockquote>
<div><p>The black box operator that flips the sign of the desired basis qubits.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><strong>Qubit</strong> : The qubits after applying the Oracle and W gate.</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Perform one iteration of grover’s algorithm to see a phase change:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.qapply</span> <span class="kn">import</span> <span class="n">qapply</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.qubit</span> <span class="kn">import</span> <span class="n">IntQubit</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.grover</span> <span class="kn">import</span> <span class="n">OracleGate</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.grover</span> <span class="kn">import</span> <span class="n">superposition_basis</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.grover</span> <span class="kn">import</span> <span class="n">grover_iteration</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">numqubits</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">basis_states</span> <span class="o">=</span> <span class="n">superposition_basis</span><span class="p">(</span><span class="n">numqubits</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">qubits</span><span class="p">:</span> <span class="n">qubits</span> <span class="o">==</span> <span class="n">IntQubit</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">OracleGate</span><span class="p">(</span><span class="n">numqubits</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qapply</span><span class="p">(</span><span class="n">grover_iteration</span><span class="p">(</span><span class="n">basis_states</span><span class="p">,</span> <span class="n">v</span><span class="p">))</span>
<span class="go">|2&gt;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.physics.quantum.grover.superposition_basis">
<span class="sig-prename descclassname"><span class="pre">sympy.physics.quantum.grover.</span></span><span class="sig-name descname"><span class="pre">superposition_basis</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">nqubits</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/physics/quantum/grover.py#L29-L55"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.physics.quantum.grover.superposition_basis" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates an equal superposition of the computational basis.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>nqubits</strong> : int</p>
<blockquote>
<div><p>The number of qubits.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><strong>state</strong> : Qubit</p>
<blockquote>
<div><p>An equal superposition of the computational basis with nqubits.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<p>Create an equal superposition of 2 qubits:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.quantum.grover</span> <span class="kn">import</span> <span class="n">superposition_basis</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">superposition_basis</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">|0&gt;/2 + |1&gt;/2 + |2&gt;/2 + |3&gt;/2</span>
</pre></div>
</div>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="gate.html"
                        title="previous chapter">Gates</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="qft.html"
                        title="next chapter">QFT</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/modules/physics/quantum/grover.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="qft.html" title="QFT"
             >next</a> |</li>
        <li class="right" >
          <a href="gate.html" title="Gates"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../index.html" >Physics</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="index.html" >Quantum Mechanics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Grover’s Algorithm</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/physics/quantum/grover.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:27:43 GMT -->
</html>